--------------------------------------------------------------------
-- ROCKET CASINO — Bannière animée en boucle pour CC: Tweaked
-- Thème : Team Rocket (Pokémon) détourné en casino
--
-- PRÉREQUIS : un "Advanced Monitor" (moniteur avancé, celui qui gère
-- les couleurs) connecté à l'ordinateur qui exécute ce programme.
-- Sur un moniteur normal (blanc/gris) les couleurs ne s'afficheront pas.
--
-- INSTALLATION :
--   1. Colle ce fichier sur ton ordinateur (edit banniere puis colle,
--      ou utilise pastebin/scp).
--   2. Accroche 3x3 (ou plus) blocs de moniteur avancé au mur.
--   3. Connecte-les à l'ordinateur (câble réseau / à côté).
--   4. Lance "banniere" (ou le nom que tu as donné au fichier).
--------------------------------------------------------------------

-------------------------------
-- CONFIGURATION RAPIDE
-------------------------------
local TEXT_SCALE     = 0.5   -- plus petit = plus de "pixels" disponibles
local FRAME_DELAY     = 0.1   -- vitesse de rafraîchissement (secondes)
local ROTATION_SPEED  = 0.22  -- vitesse de rotation de la Poké Ball
local TITLE_TEXT      = "ROCKET CASINO"
local SUBTITLE_TEXT   = "   *** DRAGUN , WLODA ET SNOUB VONT VOUS FAIRE LA GUERRE ! OUI LA GUERRE ! ***   BIENVENUE AU ROCKET CASINO   *** LES JACKPOTS SONT (PRESQUE) GARANTIS ***   "

-------------------------------
-- INITIALISATION DU MONITEUR
-------------------------------
local mon = peripheral.find("monitor")
if not mon then
  error("Aucun moniteur trouve ! Connecte un Advanced Monitor a cet ordinateur.", 0)
end

mon.setTextScale(TEXT_SCALE)
mon.setBackgroundColor(colors.black)
mon.clear()

local W, H = mon.getSize()
if W < 20 or H < 10 then
  print("Astuce : utilise un moniteur plus grand (au moins 3x3 blocs) pour un meilleur rendu.")
end

-------------------------------
-- OUTILS DE DESSIN BAS NIVEAU (canvas pixel + blit)
-------------------------------

local function newCanvas(bg)
  local c = {}
  for y = 1, H do
    c[y] = {}
    for x = 1, W do
      c[y][x] = { bg = bg, ch = " ", fg = colors.white }
    end
  end
  return c
end

-- Peint un "pixel" (case colorée) ; si ch/fg fournis, ecrit aussi un caractere
local function px(canvas, x, y, bg, ch, fg)
  x, y = math.floor(x + 0.5), math.floor(y + 0.5)
  if x >= 1 and x <= W and y >= 1 and y <= H then
    local cell = canvas[y][x]
    if bg then cell.bg = bg end
    if ch then
      cell.ch = ch
      cell.fg = fg or cell.fg
    end
  end
end

local function render(canvas)
  local toBlit = colors.toBlit
  for y = 1, H do
    local row = canvas[y]
    local chars, fgs, bgs = {}, {}, {}
    for x = 1, W do
      local cell = row[x]
      chars[x] = cell.ch
      fgs[x] = toBlit(cell.fg)
      bgs[x] = toBlit(cell.bg)
    end
    mon.setCursorPos(1, y)
    mon.blit(table.concat(chars), table.concat(fgs), table.concat(bgs))
  end
end

-------------------------------
-- PRIMITIVES DE FORMES
-------------------------------

local function fillEllipse(canvas, cx, cy, rx, ry, color)
  if rx <= 0 or ry <= 0 then return end
  for y = math.floor(cy - ry), math.ceil(cy + ry) do
    local dy = (y - cy) / ry
    local t = 1 - dy * dy
    if t >= 0 then
      local dx = rx * math.sqrt(t)
      for x = math.floor(cx - dx), math.ceil(cx + dx) do
        px(canvas, x, y, color)
      end
    end
  end
end

-- Triangle pointant vers le haut (pour les pointes/epines)
local function fillTriangleUp(canvas, cx, baseY, tipY, halfWidth, color)
  local height = baseY - tipY
  if height <= 0 then return end
  for y = tipY, baseY do
    local f = (y - tipY) / height
    local w = halfWidth * f
    for x = math.floor(cx - w), math.ceil(cx + w) do
      px(canvas, x, y, color)
    end
  end
end

-- Triangle horizontal (pour les "moustaches" laterales)
local function fillTriangleSide(canvas, baseX, tipX, cy, halfHeight, color)
  local width = tipX - baseX
  if width == 0 then return end
  local step = width > 0 and 1 or -1
  for x = baseX, tipX, step do
    local f = (x - baseX) / width
    local hh = halfHeight * (1 - f)
    for y = math.floor(cy - hh), math.ceil(cy + hh) do
      px(canvas, x, y, color)
    end
  end
end

-- Petit alphabet en pixels (utilisé pour le "R" du logo / de la Poke Ball)
local R_PATTERN = {
  "####.",
  "#...#",
  "#...#",
  "####.",
  "#..#.",
  "#...#",
  "#...#",
}

local function drawGlyphR(canvas, x0, y0, scale, color)
  for row = 1, #R_PATTERN do
    local line = R_PATTERN[row]
    for col = 1, #line do
      if line:sub(col, col) == "#" then
        for sy = 0, scale - 1 do
          for sx = 0, scale - 1 do
            px(canvas, x0 + (col - 1) * scale + sx, y0 + (row - 1) * scale + sy, color)
          end
        end
      end
    end
  end
end

local function drawText(canvas, x, y, text, fg, bg)
  for i = 1, #text do
    px(canvas, x + i - 1, y, bg, text:sub(i, i), fg)
  end
end

-------------------------------
-- ÉLÉMENTS DE LA SCÈNE
-------------------------------

-- Bordure "néon" façon enseigne de casino, ampoules qui defilent
local CHASE_COLORS = { colors.yellow, colors.orange, colors.red, colors.orange }
local function drawMarquee(canvas, tick)
  local n = #CHASE_COLORS
  for x = 1, W do
    local c = CHASE_COLORS[((x + tick) % n) + 1]
    px(canvas, x, 1, c)
    px(canvas, x, H, c)
  end
  for y = 1, H do
    local c = CHASE_COLORS[((y + tick) % n) + 1]
    px(canvas, 1, y, c)
    px(canvas, W, y, c)
  end
end

-- Titre du casino avec ombre + pulsation de couleur
local TITLE_COLORS = { colors.red, colors.orange, colors.yellow, colors.orange }
local function drawTitle(canvas, tick)
  local x = math.max(2, math.floor((W - #TITLE_TEXT) / 2) + 1)
  local y = 3
  local color = TITLE_COLORS[(math.floor(tick / 3) % #TITLE_COLORS) + 1]
  drawText(canvas, x + 1, y + 1, TITLE_TEXT, colors.black, colors.black) -- ombre
  drawText(canvas, x, y, TITLE_TEXT, color, colors.black)
end

-- Bandeau defilant en bas ("Prepare for trouble...")
local function drawSubtitle(canvas, tick)
  local y = H - 2
  local len = #SUBTITLE_TEXT
  local offset = tick % len
  local scrolled = SUBTITLE_TEXT:sub(offset + 1) .. SUBTITLE_TEXT:sub(1, offset)
  local visible = scrolled:sub(1, math.max(0, W - 4))
  drawText(canvas, 3, y, visible, colors.yellow, colors.black)
end

-- Logo fixe : glow + silhouette a pointes + grand "R" (inspire de l'image fournie)
local function drawLogo(canvas, cx, cy, r)
  fillEllipse(canvas, cx, cy, r * 1.25, r * 1.25, colors.black)
  fillEllipse(canvas, cx, cy, r * 0.95, r * 0.95, colors.red)
  fillEllipse(canvas, cx, cy, r * 0.65, r * 0.65, colors.pink)
  fillEllipse(canvas, cx, cy, r * 0.55, r * 0.55, colors.lightGray)

  -- couronne de pointes sur le dessus (oreilles/epines)
  local spikeOffsets = { -0.5, -0.25, 0, 0.25, 0.5 }
  for _, off in ipairs(spikeOffsets) do
    local sx = cx + off * r * 1.1
    local baseY = cy - r * 0.35
    local tipY = cy - r * 1.05
    fillTriangleUp(canvas, sx, baseY, tipY, r * 0.14, colors.lightGray)
  end

  -- deux pointes laterales ("moustaches")
  fillTriangleSide(canvas, cx - r * 0.55, cx - r * 1.2, cy, r * 0.12, colors.lightGray)
  fillTriangleSide(canvas, cx + r * 0.55, cx + r * 1.2, cy, r * 0.12, colors.lightGray)

  -- grand "R" rouge avec ombre noire
  local scale = math.max(1, math.floor(r / 6))
  drawGlyphR(canvas, cx - 2 * scale + 1, cy - 3 * scale + 1, scale, colors.black)
  drawGlyphR(canvas, cx - 2 * scale, cy - 3 * scale, scale, colors.red)
end

-- Poke Ball animee : palette inversee (noir en haut / rouge en bas) qui tourne
local function drawPokeball(canvas, cx, cy, r, angle)
  local squish = math.max(0.12, math.abs(math.cos(angle)))
  local rx = r * squish
  local ry = r

  -- halo derriere la balle
  fillEllipse(canvas, cx, cy, rx + 1.2, ry + 1.2, colors.gray)

  for y = math.floor(cy - ry), math.ceil(cy + ry) do
    local dy = (y - cy) / ry
    local t = 1 - dy * dy
    if t >= 0 then
      local dx = rx * math.sqrt(t)
      local color
      if y < cy - ry * 0.08 then
        color = colors.black       -- hemisphere du haut : NOIR (version Rocket)
      elseif y > cy + ry * 0.08 then
        color = colors.red         -- hemisphere du bas : ROUGE
      else
        color = colors.lightGray   -- bande centrale
      end
      for x = math.floor(cx - dx), math.ceil(cx + dx) do
        px(canvas, x, y, color)
      end
    end
  end

  -- ligne centrale noire
  for x = math.floor(cx - rx), math.ceil(cx + rx) do
    px(canvas, x, cy, colors.black)
  end

  -- bouton central
  local buttonR = math.max(1, r * 0.28 * squish)
  fillEllipse(canvas, cx, cy, buttonR + 0.6, buttonR + 0.6, colors.black)
  fillEllipse(canvas, cx, cy, buttonR, buttonR, colors.white)
  fillEllipse(canvas, cx, cy, buttonR * 0.5, buttonR * 0.5, colors.lightGray)

  -- le "R" n'apparait que quand la balle fait face a la camera (effet de flash)
  if squish > 0.55 then
    local scale = math.max(1, math.floor(r * squish / 5))
    drawGlyphR(canvas, math.floor(cx - 2 * scale), math.floor(cy - ry * 0.75), scale, colors.red)
  end
end

-------------------------------
-- BOUCLE PRINCIPALE
-------------------------------

local tick = 0
local angle = 0

while true do
  local canvas = newCanvas(colors.black)

  drawMarquee(canvas, tick)
  drawTitle(canvas, tick)

  local logoR = math.floor(math.min(W, H) * 0.18)
  drawLogo(canvas, math.floor(W * 0.28), math.floor(H * 0.55), logoR)

  local ballR = math.floor(math.min(W, H) * 0.16)
  drawPokeball(canvas, math.floor(W * 0.72), math.floor(H * 0.55), ballR, angle)

  drawSubtitle(canvas, tick)

  render(canvas)

  angle = angle + ROTATION_SPEED
  tick = tick + 1
  sleep(FRAME_DELAY)
end
